<?php

namespace App\Model\Guild;

use App\Model\CoinLog;
use App\Model\UserActLog;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;

/**
 * 公会结算日报
 */
class GuildAccountDaily extends GuildBase
{
    protected $table = 'guild_account_daily';

    protected $fillable = [
        'dt', 'guild_manage_id', 'total_turnover', 'voice_turnover', 'video_turnover', 'active_host_num', 'income_host_num', 'host_share_income', 'guild_share_income', 'host_total_share', 'msg_turnover', 'gift_turnover', 'free_turnover', 'voice_income', 'video_income', 'gift_income', 'msg_income', 'invite_recharge_income'
    ];

    /**
     * 公会结算
     */
    static function guildAccount($guild = [], $hostIds = [], $startTime = '', $endTime = '')
    {
        $info = [];
        $info['dt'] = $startTime;
        //公会ID
        $info['guild_manage_id'] = $guild['id'];
        //计算当天活跃
        $info['active_host_num'] = UserActLog::checkActiveCount($hostIds, $startTime, $endTime);
        //收益主播数量
        $incomeHostIds = CoinLog::checkHostCount($hostIds, $startTime, $endTime, 'uid');
        $info['income_host_num'] = count($incomeHostIds);
        //语音流水
        // $info['voice_turnover'] = CoinLog::getTotalConsume($hostIds, $startTime, $endTime, [CoinLog::FROM_VOICECALL_OUT, CoinLog::FROM_QUICK_VOICE_OUT]);
        $info['voice_income'] = CoinLog::getTotalIncome($hostIds, $startTime, $endTime, [CoinLog::FROM_VOICECALL_IN]);
        // //视频流水
        // $info['video_turnover'] = CoinLog::getTotalConsume($hostIds, $startTime, $endTime, [CoinLog::FROM_VIDEOCALL_OUT, CoinLog::FROM_QUICK_VIDEO_OUT]);
        $info['video_income'] = CoinLog::getTotalIncome($hostIds, $startTime, $endTime, [CoinLog::FROM_VIDEOCALL_IN, CoinLog::FROM_DAILY_FREE]);
        // //私信流水
        // $info['msg_turnover'] = CoinLog::getTotalConsume($hostIds, $startTime, $endTime, CoinLog::FROM_SEND_PRIVATE_MSG_OUT);
        $info['msg_income'] = CoinLog::getTotalIncome($hostIds, $startTime, $endTime, [CoinLog::FROM_SEND_PRIVATE_MSG_IN]);
        // //礼物流水
        // $info['gift_turnover'] =  CoinLog::getTotalConsume($hostIds, $startTime, $endTime, CoinLog::FROM_SEND_GIFT);
        $info['gift_income'] = CoinLog::getTotalIncome($hostIds, $startTime, $endTime, [CoinLog::FROM_GET_GIFT]);
        // //平台福利
        // $info['free_turnover'] = CoinLog::getTotalIncome($hostIds, $startTime, $endTime, CoinLog::FROM_DAILY_FREE);
//        $info['invite_recharge_income'] = CoinLog::getTotalIncome($hostIds, $startTime, $endTime, [CoinLog::FROM_INVITE_RECHARGE]);
        $info['invite_recharge_income'] = 0;
        // //总流水（总消费）
        // $info['total_turnover'] = CoinLog::getTotalConsume($hostIds, $startTime, $endTime);
        //主播总收入
        // $info['host_total_share'] = CoinLog::getTotalIncome($hostIds, $startTime, $endTime);
        //计算分成的部分（语音和视频）
        // $shareTotal = $info['voice_turnover'] + $info['video_turnover'] + $info['msg_turnover'] + $info['gift_turnover'] + $info['free_turnover'];
        //主播分成（语音，视频，私信，普通礼物）
        // $info['host_share_income'] = GuildManage::hostShareIncome($guild['host_share_ratio'], $shareTotal);
        //公会分成（语音，视频，私信，普通礼物）
        // $info['guild_share_income'] = GuildManage::guildShareIncome($guild['guild_share_ratio'], $shareTotal);
        //添加记录
        log_info(__CLASS__ . "==statGuildAccount==GuildId:{$guild['id']}==HostIds:" . json_encode($hostIds) . "==" . json_encode($info));

        GuildAccountDaily::updateOrCreate([
            'guild_manage_id' => $guild['id'],
            'dt' => $info['dt']
        ], $info);

        $share = (new GuildManage)->where([
            'id' => $guild['id'],
        ])->value('guild_share_ratio');

        $current = self::where([
            'guild_manage_id' => $guild['id'],
            'dt' => $info['dt']
        ])->value('share_ratio');

        if ($share > 0 && $current == 0) {
            self::where([
                'guild_manage_id' => $guild['id'],
                'dt' => $info['dt']
            ])->update([
                'share_ratio' => $share
            ]);
        }

        return $incomeHostIds;
    }

    /**
     * @param int $guildId
     * @return Builder|Model|object|null
     */
    public function getYesterdayDataByGuild(int $guildId)
    {
        return $this
            ->where('dt', date('Y-m-d', strtotime("-1 day")))
            ->where('guild_manage_id', $guildId)
            ->first();
    }
}
